package org.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class algorithm_Demo1 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number: ");
        while (true) {
            int num = sc.nextInt();
            list.add(num);
            if(list.size()==10){
                break;
            }
        }
        //对列表进行排序
        Collections.sort(list);
        System.out.println(list);
        System.out.println("Enter number to search: ");
        int number = sc.nextInt();
        System.out.println(halfSearch(list, number));
    }
//二分查找
    public static String halfSearch(ArrayList<Integer> list, int number) {
        int min = 0;
        int max = list.size() - 1;
        while (true) {
            if(min>max){
                return null;
            }
            int mid = (max + min) / 2;
            if (list.get(mid) < number) {
                min = mid + 1;
            } else if (list.get(mid)> number) {
                max = mid - 1;
            } else {
                return mid+" "+list.get(mid);
            }
        }

    }
}
